home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / GLUT-3.7 / PROGS / DEMOS / SMOOTH / TB.C < prev    next >
Encoding:
C/C++ Source or Header  |  1998-08-12  |  2.4 KB  |  148 lines

  1. /*
  2.  *  Simple trackball-like motion adapted (ripped off) from projtex.c
  3.  *  (written by David Yu and David Blythe).  See the SIGGRAPH '96
  4.  *  Advanced OpenGL course notes.
  5.  */
  6.  
  7.  
  8. /* includes */
  9. #include <math.h>
  10. #include <assert.h>
  11. #include <GL/glut.h>
  12. #include "tb.h"
  13. #include "trackball.h"
  14.  
  15. /* globals */
  16. static GLuint    tb_lasttime;
  17.  
  18. float curquat[4];
  19. float lastquat[4];
  20. int beginx, beginy;
  21.  
  22. static GLuint    tb_width;
  23. static GLuint    tb_height;
  24.  
  25. static GLint     tb_button = -1;
  26. static GLboolean tb_tracking = GL_FALSE;
  27. static GLboolean tb_animate = GL_TRUE;
  28.  
  29. void
  30. tbStepAnimation(void)
  31. {
  32.   add_quats(lastquat, curquat, curquat);
  33. }
  34.  
  35. void
  36. _tbAnimate(void)
  37. {
  38.   tbStepAnimation();
  39.   glutPostRedisplay();
  40. }
  41.  
  42. static void
  43. defaultAnimateFunc(int animate)
  44. {
  45.   if (animate) {
  46.     glutIdleFunc(_tbAnimate);
  47.   } else {
  48.     glutIdleFunc(0);
  49.   }
  50. }
  51.  
  52. void (*animateFunc)(int animate) = defaultAnimateFunc;
  53.  
  54. void
  55. _tbStartMotion(int x, int y, int time)
  56. {
  57.   assert(tb_button != -1);
  58.  
  59.   animateFunc(0);
  60.   tb_tracking = GL_TRUE;
  61.   tb_lasttime = time;
  62.   beginx = x;
  63.   beginy = y;
  64. }
  65.  
  66. void
  67. _tbStopMotion(unsigned time)
  68. {
  69.   assert(tb_button != -1);
  70.  
  71.   tb_tracking = GL_FALSE;
  72.  
  73.   if (time == tb_lasttime && tb_animate) {
  74.     animateFunc(1);
  75.   } else {
  76.     if (tb_animate) {
  77.       animateFunc(0);
  78.     }
  79.   }
  80. }
  81.  
  82. void
  83. tbAnimate(GLboolean animate)
  84. {
  85.   tb_animate = animate;
  86. }
  87.  
  88. void
  89. tbInit(GLuint button)
  90. {
  91.   tb_button = button;
  92.   trackball(curquat, 0.0, 0.0, 0.0, 0.0);
  93. }
  94.  
  95. void
  96. tbMatrix(void)
  97. {
  98.   GLfloat m[4][4];
  99.  
  100.   assert(tb_button != -1);
  101.   build_rotmatrix(m, curquat);
  102.   glMultMatrixf(&m[0][0]);
  103. }
  104.  
  105. void
  106. tbReshape(int width, int height)
  107. {
  108.   assert(tb_button != -1);
  109.  
  110.   tb_width  = width;
  111.   tb_height = height;
  112. }
  113.  
  114. void
  115. tbMouse(int button, int state, int x, int y)
  116. {
  117.   assert(tb_button != -1);
  118.  
  119.   if (state == GLUT_DOWN && button == tb_button)
  120.     _tbStartMotion(x, y, glutGet(GLUT_ELAPSED_TIME));
  121.   else if (state == GLUT_UP && button == tb_button)
  122.     _tbStopMotion(glutGet(GLUT_ELAPSED_TIME));
  123. }
  124.  
  125. void
  126. tbMotion(int x, int y)
  127. {
  128.   if (tb_tracking) {
  129.     trackball(lastquat,
  130.       (2.0 * beginx - tb_width) / tb_width,
  131.       (tb_height - 2.0 * beginy) / tb_height,
  132.       (2.0 * x - tb_width) / tb_width,
  133.       (tb_height - 2.0 * y) / tb_height
  134.       );
  135.     beginx = x;
  136.     beginy = y;
  137.     tb_animate = 1;
  138.     tb_lasttime = glutGet(GLUT_ELAPSED_TIME);
  139.     tbStepAnimation();
  140.   }
  141. }
  142.  
  143. void
  144. tbAnimateFunc(void (*func)(int animate))
  145. {
  146.   animateFunc = func;
  147. }
  148.